package cn.edu.hunau.java.week03.day01;

import org.junit.Test;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ConcurrentLinkedDeque;

public class PraseIdTest {
    @Test
    public void testPraseId(){
        int age=parseId("430431200705260001");
        System.out.println("年龄为:"+age);
    }
    public int parseId(String id){
        String idRegex="^\\d{17}[0-9Xx]$";
        if(!id.matches(idRegex)){
            throw new RuntimeException("身份证格式不正确");
        }
        String birth=id.substring(6,14);
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate birthDate=LocalDate.parse(birth,dtf);
        LocalDate now=LocalDate.now();
        int year=now.getYear();
        int month=now.getMonthValue();
        int yearBirth=birthDate.getYear();
        int monthBirth=birthDate.getMonthValue();
        int age = year - yearBirth;
        if(month < monthBirth){
            age--;
        }
        return age;
    }
}
